package io.sage.modules.basics.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.sage.modules.basics.dao.BGoodsAllocationMapper;
import io.sage.modules.basics.entity.BGoodsAllocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.sage.modules.basics.dao.BGoodsShelvesMapper;
import io.sage.modules.basics.entity.BGoodsShelves;
import io.sage.modules.basics.service.BGoodsShelvesService;


@Service("goodsShelvesService")
public class BGoodsShelvesServiceImpl implements BGoodsShelvesService {
	
	@Autowired
	private BGoodsShelvesMapper goodsShelvesMapper;
	@Autowired
	private BGoodsAllocationMapper goodsAllocationMapper;
	/**
     * 货架列表
     * @param params
     * @return
     */
	@Override
	public List<BGoodsShelves> selectGoodsShelvesList(Map<String, Object> params) {
		return goodsShelvesMapper.selectGoodsShelvesList(params);
	}
	
	/**
     * 批量删除
     * @param ids
     * @return
     */
	@Override
	public int deleteBatchIds(Integer[] ids) {
		return goodsShelvesMapper.deleteBatchIds(ids);
	}
	
	/**
	 * 根据主键id查询货架信息
	 * @param id
	 * @return
	 */
	@Override
	public BGoodsShelves selectByPrimaryKey(Integer id) {
		return goodsShelvesMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 添加货架信息
	 * @param goodsShelves
	 * @return
	 */
	@Override
	public int insert(BGoodsShelves goodsShelves) {
		goodsShelves.setCreateTime(new Date());
		goodsShelves.setUpdateTime(new Date());
		goodsShelves.setIsDel(0);
		goodsShelves.setIsUsed(1);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
		String date=simpleDateFormat.format(new Date());
		goodsShelves.setBarCode(date);
		goodsShelves.setShelveCode("HJ-"+date);
		int i=goodsShelvesMapper.insertSelective(goodsShelves);
		List<BGoodsAllocation>list=new ArrayList<>();
		for (int j = 0; j < goodsShelves.getSpecsRow(); j++) {
			for (int k = 0; k <goodsShelves.getSpecsCol() ; k++) {
				BGoodsAllocation bGoodsAllocation=new BGoodsAllocation();
				bGoodsAllocation.setUpdateTime(new Date());
				bGoodsAllocation.setCreateTime(new Date());
				bGoodsAllocation.setCreateUserId(goodsShelves.getCreateUserId()+"");
				bGoodsAllocation.setIsDel(0);
				bGoodsAllocation.setIsUsed(1);
				bGoodsAllocation.setGoodsShelveId(goodsShelves.getId());
				bGoodsAllocation.setAllocationName((j+1)+"行"+(k+1)+"列");
				bGoodsAllocation.setAllocationCode("HW"+goodsShelves.getWareHouseId()+"-"+goodsShelves.getGoodsAreaId()+"-"+goodsShelves.getId()+"-"+(j+1)+"-"+(k+1));
				bGoodsAllocation.setBarCode(goodsShelves.getWareHouseId()+""+goodsShelves.getGoodsAreaId()+""+goodsShelves.getId()+""+(j+1)+""+(k+1));
				bGoodsAllocation.setGoodsShelveCol((k+1));
				bGoodsAllocation.setGoodsShelveRow((j+1));
				list.add(bGoodsAllocation);
			}
		}
		i=goodsAllocationMapper.insertBatch(list);
		return i;
	}
	
	/**
	 * 根据主键id，修改货架信息
	 * @param goodsShelves
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(BGoodsShelves goodsShelves) {
		goodsShelves.setUpdateTime(new Date());
		return goodsShelvesMapper.updateByPrimaryKeySelective(goodsShelves);
	}

}
